import {add${entity}, delete${entity}, query${entity}, update${entity}} from '@${servicePath}'
import {storePrompt} from '@/store/modules/commonStore'
import {filter} from 'lodash'

export default {
  namespaced: true,
  state: {
    title: '',
    loading: false,
    tableLoading: false,
    tableData: [],
    editData: null,
    modelVisible: false,
    pagination: {},
    queryForm: {}
  },
  mutations: {
    setEditData(state, payload) {

      state.editData = filter(state.tableData, {objectId: payload})[0]
    }
  },
  actions: {
    async query({commit, state}, payload) {
      payload = {...payload, ...state.queryForm}
      await query${entity}(payload).then((obj) => {
        let {data} = obj.data
        let {total, current} = data
        commit('setState', {
          name: 'pagination',
          value: {...state.pagination, total: Number(total), current: Number(current)}
        })
        commit('setState', {name: 'tableData', value: data.records})
      })
      commit('setState', {name: 'tableLoading', value: false})
    },

    async add({commit, dispatch}, payload) {
      await add${entity}(payload).then((obj) => {
        let {success} = obj.data
        storePrompt(success)
      })
      commit('setState', {name: 'loading', value: false})
      dispatch('query')
    },

    async update({commit, dispatch}, payload) {
      await update${entity}(payload).then((obj) => {
        let {success} = obj.data
        storePrompt(success)
      })
      commit('setState', {name: 'loading', value: false})
      dispatch('query')
    },

    async delete({commit, dispatch}, payload) {
      await delete${entity}(payload).then((obj) => {
        let {success} = obj.data
        storePrompt(success)
      })
      dispatch('query')
    },

  }
}